import { createClient } from "supabase-wechat-stable-v2";
import store from "../store";

const supaKey =
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImV4cCI6MzMyOTY5Mjc4OSwiaWF0IjoxNzUyODkyNzg5LCJpc3MiOiJzdXBhYmFzZSJ9.TaXQcfrWthoXwwtJnAj_nDIncaCfCu-mPPkG9BU_Yok";

export const supabase = createClient(
  "https://d1tgat8g91hlvpqa3k3g.baseapi.memfiredb.com",
  supaKey
);

export function getSbData(res, showTip = true, opts = {}) {
  const { error, data } = res;
  if (error) {
    const { getErrMsg } = opts;
    let msg = error.message;
    if (error.status == 401) {
      store.dispatch("logout");
      msg = "登录状态已失效";
    }
    if (getErrMsg) {
      msg = getErrMsg(error);
    }
    if (showTip) {
      uni.$u.toast(msg);
    }
    throw error;
  }
  return data;
}
